export const phoneRules = [
  { required: true, message: '请输入手机号' },
  { validator: val => /1\d{10}/.test(val), message: '手机号格式错误' }
]

export const passwordRules = [
  { required: true, message: '请输入密码' },
  { validator: val => val.length >= 6 && val.length <= 20, message: '密码长度为6-20位' }
]

export const payPasswordRules = [
  { required: true, message: '请输入密码' },
  { validator: val => val.length === 6, message: '支付密码长度为6位' },
  { validator: val => /^[\d]*$/.test(val), message: '支付密码必须都是数字' }
]

export const idCardNumberRules = [
  { required: true, message: '请输入身份证号码' },
  {
    validator: val => /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}[\dXx]$/.test(val),
    message: '请输入正确的身份证号码'
  }
]

/**
 * 检验方法
 * @param {Array} rules 需要检验的规则
 * @param {Any} value  需要检验的值
 * @return {Object} {state,errorMsg} state:为true验证不通过 errorMsg:错误信息
 */
export const checkFn = (rules, value) => {
  let error = ''
  let state = rules.some(item => {
    if (item.required && (value === '' || !value)) {
      error = item.message
      return true
    } else if (item.validator && !item.validator(value)) {
      error = item.message
      return true
    }
  })
  return { state, error }
}
